package cn.edu.ldu.communityqa.communityqa.controller;

import cn.edu.ldu.communityqa.communityqa.entity.Question;
import cn.edu.ldu.communityqa.communityqa.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/question")
public class QuestionController {
    private final QuestionService questionService;

    @Autowired
    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    @GetMapping
    public List<Question> findAll() {
        return questionService.findAll();
    }

    @GetMapping("/{id}")
    public Question findById(@PathVariable Long id) {
        return questionService.findById(id);
    }

    // post 新增
    @PostMapping
    public int save(@RequestBody Question question) {
        return questionService.save(question);
    }

    @PutMapping
    public int update(@RequestBody Question question) {
        return questionService.update(question);
    }

    //findByTitleLike
    @GetMapping("/title-like/{title}")
    public List<Question> findByTitleLike(@PathVariable String title) {
        return questionService.findByTitleLike(title);
    }

    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Long id) {
        return questionService.deleteById(id);
    }

}
